package jdmx.library.da;

import java.util.ArrayList;
import java.util.Scanner;

public class SqHelper {

	public static ArrayList<String> getParamsFromSql(String sql) {
		String seps = ",|\\s|\n|\r|\t";
		Scanner scaner  = new Scanner(sql);
		scaner.useDelimiter(seps);
		ArrayList<String> paramNames = new ArrayList<String>();
		while (scaner.hasNext()) {
			String token = scaner.next();
			if (token.length() == 0) continue;
			if (!(token.startsWith("@"))) continue;
			token = token.substring(1);
			if (token.length() == 0) continue;
			paramNames.add(token);
		}
		return paramNames;
	}

	public static String replaceParamsIntoSql(String sql) {
		ArrayList<String> paramNames = getParamsFromSql(sql);
		String newSql = sql;
		for (String paramName : paramNames) {
			String token = "@" + paramName;
			newSql = newSql.replaceAll(token, "?");
		}
		return newSql;
	}
}
